import { useRequest } from "../../../utils/request";
import dayjs from "../../../utils/day";
import map from "../../../utils/map";

Component({
    properties: {},
    data: {
        statusMap: {
            1: '待支付',
            2: '待入住',
            3: '入住中',
            4: '退房中',
            5: '已退房',
        },
        // 退押金选项
        reMoneyStatus: 1,
        // 退押金数额
        reMoney: 0,
    },
    observers: {
        data: function (data) {
            this.setData({
                addressText: data.province + data.city + data.area + data.address
            })
        }
    },
    methods: {
        onLoad(options) {
            // 如果是房东
            if (options.landlord) {
                this.setData({
                    landlord: true
                })
                this.removeRequest = new useRequest('api/ownerdelorder', { hold: true }, this)
                this.agreeRetreatRequest = new useRequest('api/owneragreeback', { hold: true }, this)
                this.retreatRequest = new useRequest('api/ownerorderback', { hold: true, loading: false }, this)
                this.reMoneyRequest = new useRequest('api/ownerrefundyajin', { hold: true, loading: false }, this)
            } else {
                this.retreatRequest = new useRequest('api/userapplyback', { hold: true, loading: false }, this)
                this.cancelRequest = new useRequest('api/usercancernopayorder', { hold: true, loading: false }, this)
            }

            this.request = new useRequest('api/orderdetail', {
                params: { order_no: options.id },
                pretreatment: data => {
                    data.start = dayjs.unix(data.live_in_time).format('MM.DD')
                    data.startDay = dayjs.unix(data.live_in_time).format('YYYY-MM-DD HH:mm:ss')
                    data.end = dayjs.unix(data.live_out_time).format('MM.DD')
                    data.endDay = data.live_out_actual_time ? dayjs.unix(data.live_out_actual_time).format('YYYY-MM-DD HH:mm:ss') : '---------'
                    data.payTime = data.pay_time ? dayjs.unix(data.pay_time).format('YYYY-MM-DD HH:mm:ss') : '---------'

                    // 是否显示退房
                    this.setData({
                        showComment: dayjs().isBetween(
                            dayjs.unix(data.live_out_actual_time),
                            dayjs.unix(data.live_out_actual_time).add(7, 'day'),
                            'day',
                            '[]'
                        )
                    })
                    return data
                },
                data: 'data'
            }, this)
        },
        copy() {
            wx.setClipboardData({
                data: this.data.data.order_no
            })
        },
        async onPullDownRefresh() {
            await this.request.run()
            wx.stopPullDownRefresh()
        },
        // 周边
        seeMap() {
            map.geocoder({
                address: this.data.addressText,
                success: res => {
                    const { lat, lng } = res.result.location
                    wx.openLocation({
                        latitude: lat,
                        longitude: lng,
                        name: this.data.data.title,
                        address: this.data.addressText
                    })
                }
            })
        },
        // 退房
        retreat() {
            wx.showModal({
                title: '是否申请退房',
                content: '退房之后将不可再入住',
                success: async result => {
                    if (result.confirm) {
                        wx.showLoading({})
                        await this.retreatRequest.run({ order_no: this.data.data.order_no })
                        wx.hideLoading()
                        wx.startPullDownRefresh()
                    }
                }
            })
        },
        // 同意退房
        agreeRetreat() {
            wx.showModal({
                title: '是否同意退房',
                content: '退房之后将不可再入住',
                success: async result => {
                    if (result.confirm) {
                        wx.showLoading({})
                        await this.agreeRetreatRequest.run({ order_no: this.data.data.order_no })
                        wx.hideLoading()
                        wx.startPullDownRefresh()
                    }
                }
            })
        },
        // 联系
        callPhone() {
            wx.makePhoneCall({
                phoneNumber: this.data.landlord
                    ? this.data.data.user_phone
                    : this.data.data.house_owner_phone
            })
        },
        // 删除订单
        remove() {
            wx.showModal({
                title: '是否删除订单',
                content: '删除订单后不可找回',
                success: async result => {
                    if (result.confirm) {
                        wx.showLoading({})
                        await this.removeRequest.run({ order_no: this.data.data.order_no })
                        wx.hideLoading()
                        wx.navigateBack()
                    }
                }
            })
        },
        // 评价
        comment() {
            wx.navigateTo({
                url: '/pages/order/comment/edit/edit?id=' + this.data.data.order_no
            })
        },
        cancel() {
            wx.showModal({
                title: '是否取消订单',
                content: '取消之后将不可再入住',
                success: async result => {
                    if (result.confirm) {
                        wx.showLoading({})
                        await this.cancelRequest.run({ order_no: this.data.data.order_no })
                        wx.hideLoading()
                        wx.navigateBack()
                    }
                }
            })
        },
        pay() {
            wx.navigateTo({
                url: `/pages/order/pay/pay?order=${this.data.data.order_no}&price=${this.data.data.total}`
            })
        },
        // 查看评价
        readComment() {
            wx.navigateTo({
                url: '/pages/house/comment/comment?id=' + this.data.data.house_id
            })
        },
        // 退押金
        showReMoneyWindow() {
            this.setData({
                showReMoneyWindowStatus: true
            })
        },
        hideReMoneyWindow() {
            this.setData({
                showReMoneyWindowStatus: false
            })
        },
        changeReStatus(e) {
            this.setData({
                reMoneyStatus: Number(e.detail.value)
            })
        },
        async reMoney() {
            wx.showLoading({})
            await this.reMoneyRequest.run({
                order_no: this.data.data.order_no,
                type: this.data.reMoneyStatus,
                yajin: this.data.reMoney
            })
            wx.hideLoading()
            wx.startPullDownRefresh()
        }
    }
});
